///*
//** Haaf's Game Engine 1.8
//** Copyright (C) 2003-2007, Relish Games
//** hge.relishgames.com
//**
//** hge_tut03 - Using helper classes
//*/
//
//
//// Copy the files "particles.png", "menu.wav",
//// "font1.fnt", "font1.png" and "trail.psi" from
//// the folder "precompiled" to the folder with
//// executable file. Also copy hge.dll and bass.dll
//// to the same folder.
//
//
//#include "global.h"
//
//
//hgeResourceManager* resource_manager = 0;
//
//HGE *hge = 0;
//
//// Some "gameplay" variables and constants
//float x=100.0f, y=100.0f;
//float dx=0.0f, dy=0.0f;
//
//const float speed=90;
//const float friction=0.98f;
//
//// This function will be called by HGE once per frame.
//// Put your game loop code here. In this example we
//// just check whether ESC key has been pressed.
//// Play sound effect
//void boom() {
//	int pan=int((x-400)/4);
//	float pitch=(dx*dx+dy*dy)*0.0005f+0.2f;
//	//hge->Effect_PlayEx(snd,100,pan,pitch);
//}
//
//bool FrameFunc()
//{
//	float dt=hge->Timer_GetDelta();
//
//	// Process keys
//	if (hge->Input_GetKeyState(HGEK_ESCAPE)) return true;
//	if (hge->Input_GetKeyState(HGEK_LEFT)) dx-=speed*dt;
//	if (hge->Input_GetKeyState(HGEK_RIGHT)) dx+=speed*dt;
//	if (hge->Input_GetKeyState(HGEK_UP)) dy-=speed*dt;
//	if (hge->Input_GetKeyState(HGEK_DOWN)) dy+=speed*dt;
//
//	// Do some movement calculations and collision detection	
//	dx*=friction; dy*=friction; x+=dx; y+=dy;
//	if(x>784) {x=784-(x-784);dx=-dx;boom();}
//	if(x<16) {x=16+16-x;dx=-dx;boom();}
//	if(y>584) {y=584-(y-584);dy=-dy;boom();}
//	if(y<16) {y=16+16-y;dy=-dy;boom();}
//
//	// Update particle system
//	//par->info.nEmission=(int)(dx*dx+dy*dy)*2;
//	//par->MoveTo(x,y);
//	//par->Update(dt);
//
//	return false;
//}
//
//bool RenderFunc()
//{
//	hge->Gfx_BeginScene();
//	resource_manager->GetSprite("cursor")->Render(x,y);
//	hge->Gfx_EndScene();
//	return false;
//}
//
//int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
//{
//	// Here we use global pointer to HGE interface.
//	// Instead you may use hgeCreate() every
//	// time you need access to HGE. Just be sure to
//	// have a corresponding hge->Release()
//	// for each call to hgeCreate()
//	hge = hgeCreate(HGE_VERSION);
//
//	// Set our frame function
//	hge->System_SetState(HGE_FRAMEFUNC, FrameFunc);
//	hge->System_SetState(HGE_RENDERFUNC, RenderFunc);
//
//	// Set the window title
//	hge->System_SetState(HGE_TITLE, "HGE Tutorial 01 - Minimal HGE application");
//
//	// Run in windowed mode
//	// Default window size is 800x600
//	hge->System_SetState(HGE_WINDOWED, true);
//
//	// Don't use BASS for sound
//	hge->System_SetState(HGE_USESOUND, false);
//
//	// Tries to initiate HGE with the states set.
//	// If something goes wrong, "false" is returned
//	// and more specific description of what have
//	// happened can be read with System_GetErrorMessage().
//	if(hge->System_Initiate())
//	{
//		resource_manager = new hgeResourceManager("resource.txt");
//		
//		// Starts running FrameFunc().
//		// Note that the execution "stops" here
//		// until "true" is returned from FrameFunc().
//		hge->System_Start();
//	}
//	else
//	{	
//		// If HGE initialization failed show error message
//		MessageBoxA(NULL, hge->System_GetErrorMessage(), "Error", MB_OK | MB_ICONERROR | MB_APPLMODAL);
//	}
//
//	// Now ESC has been pressed or the user
//	// has closed the window by other means.
//
//	resource_manager->Purge();
//	// Restore video mode and free
//	// all allocated resources
//	hge->System_Shutdown();
//
//	delete resource_manager;
//	// Release the HGE interface.
//	// If there are no more references,
//	// the HGE object will be deleted.
//	hge->Release();
//
//	return 0;
//}

/*
** Haaf's Game Engine 1.8
** Copyright (C) 2003-2007, Relish Games
** hge.relishgames.com
**
** hge_tut01 - Minimal HGE application
*/


#include "global.h"

#include "gui_manager.h"

HGE *hge = 0;
hgeResourceManager* resource_manager = 0;

// Some "gameplay" variables and constants
float x=100.0f, y=100.0f;
float dx=0.0f, dy=0.0f;

const float speed=90;
const float friction=0.98f;


// This function plays collision sound with
// parameters based on sprite position and speed
void boom() {
	int pan=int((x-400)/4);
	float pitch=(dx*dx+dy*dy)*0.0005f+0.2f;
	//hge->Effect_PlayEx(snd,100,pan,pitch);
}

bool FrameFunc()
{
	// Get the time elapsed since last call of FrameFunc().
	// This will help us to synchronize on different
	// machines and video modes.
	float dt=hge->Timer_GetDelta();

	// Process keys
	if (hge->Input_GetKeyState(HGEK_ESCAPE)) return true;
	if (hge->Input_GetKeyState(HGEK_LEFT)) dx-=speed*dt;
	if (hge->Input_GetKeyState(HGEK_RIGHT)) dx+=speed*dt;
	if (hge->Input_GetKeyState(HGEK_UP)) dy-=speed*dt;
	if (hge->Input_GetKeyState(HGEK_DOWN)) dy+=speed*dt;

	// Do some movement calculations and collision detection	
	dx*=friction; dy*=friction; x+=dx; y+=dy;
	if(x>784) {x=784-(x-784);dx=-dx;boom();}
	if(x<16) {x=16+16-x;dx=-dx;boom();}
	if(y>584) {y=584-(y-584);dy=-dy;boom();}
	if(y<16) {y=16+16-y;dy=-dy;boom();}

	GUI_MANAGER::get_instance()->update(dt);
	// Continue execution
	return false;
}

bool RenderFunc()
{
	hge->Gfx_BeginScene();
	hge->Gfx_Clear(0);
	GUI_MANAGER::get_instance()->render();
	resource_manager->GetSprite("object")->Render(x, y);
	hge->Gfx_EndScene();
	// Continue execution
	return false;
}

int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
	// Here we use global pointer to HGE interface.
	// Instead you may use hgeCreate() every
	// time you need access to HGE. Just be sure to
	// have a corresponding hge->Release()
	// for each call to hgeCreate()
	hge = hgeCreate(HGE_VERSION);

	hge->System_SetState(HGE_LOGFILE, "hge_tut02.log");
	// Set our frame function
	hge->System_SetState(HGE_FRAMEFUNC, FrameFunc);
	hge->System_SetState(HGE_RENDERFUNC, RenderFunc);

	// Set the window title
	hge->System_SetState(HGE_TITLE, "HGE Tutorial 01 - Minimal HGE application");

	// Run in windowed mode
	// Default window size is 800x600
	hge->System_SetState(HGE_WINDOWED, true);

	// Don't use BASS for sound
	hge->System_SetState(HGE_USESOUND, false);
	hge->System_SetState(HGE_HIDEMOUSE, false);

	GUI_MANAGER::get_instance()->initialize();

	resource_manager = new hgeResourceManager("resource.txt");

	// Tries to initiate HGE with the states set.
	// If something goes wrong, "false" is returned
	// and more specific description of what have
	// happened can be read with System_GetErrorMessage().
	if(hge->System_Initiate())
	{
		// Starts running FrameFunc().
		// Note that the execution "stops" here
		// until "true" is returned from FrameFunc().
		hge->System_Start();
	}
	else
	{	
		// If HGE initialization failed show error message
		MessageBoxA(NULL, hge->System_GetErrorMessage(), "Error", MB_OK | MB_ICONERROR | MB_APPLMODAL);
	}

	// Now ESC has been pressed or the user
	// has closed the window by other means.

	delete resource_manager;
	GUI_MANAGER::get_instance()->close();

	// Restore video mode and free
	// all allocated resources
	hge->System_Shutdown();

	// Release the HGE interface.
	// If there are no more references,
	// the HGE object will be deleted.
	hge->Release();

	return 0;
}
